// Copyright Gary Evans 2006.

using System;

using Tao.OpenGl;

using Taumuon.Jabuka.Physics;
using System.Drawing;

namespace Taumuon.Jabuka.MainApp
{
    class Sphere : IDrawable
    {
        #region Object Lifetime

        /// <summary>
        /// Construct a drawable Sphere.
        /// </summary>
        /// <param name="eulerRigidBody">
        /// The rigid body that is used to obtain the
        ///  Sphere's location.
        /// </param>
        /// <exception cref="ArgumentNullException"/>
        public Sphere(EulerRigidBody eulerRigidBody)
        {
            if (eulerRigidBody == null)
            {
                throw new ArgumentNullException("eulerRigidBody");
            }
            this.eulerRigidBody = eulerRigidBody;
        }

        #endregion Object Lifetime

        #region Member Variables

        EulerRigidBody eulerRigidBody = null;

        #endregion Member Variables

        private int dimension
        {
            get
            {
                return 25;
            }
        }
        public void Draw()
        {
           // return;
            Gl.glPushMatrix();
            Gl.glTranslated(eulerRigidBody.Location.X + eulerRigidBody.ScreenTranslate.X,
                eulerRigidBody.Location.Y + eulerRigidBody.ScreenTranslate.Y,
                eulerRigidBody.Location.Z + eulerRigidBody.ScreenTranslate.Z);
        //    Gl.glRotatef(90, 1,0,0);
            bool drawWireFrame = false;
            int drawStyle = drawWireFrame ? Glu.GLU_LINE :
                                                  Glu.GLU_FILL;

            double yOffset = 0;

           // const float axisSize = dimension;

            // draw a red line along the z-axis
            //Gl.glColor3f(1.0f, 0.0f, 0.0f);
            //Gl.glBegin(Gl.GL_LINES);
            //Gl.glVertex3f(0.0f, 0.0f, -dimension);
            //Gl.glVertex3f(0.0f, 0.0f, dimension);
            //Gl.glEnd();

            //// draw a green line along the y-axis
            //Gl.glColor3f(0.0f, 1.0f, 0.0f);
            //Gl.glBegin(Gl.GL_LINES);
            //Gl.glVertex3f(0.0f, -dimension, 0.0f);
            //Gl.glVertex3f(0.0f, dimension, 0.0f);
            //Gl.glEnd();

            //// draw a blue line along the x-axis
            //Gl.glColor3f(0.0f, 0.0f, 1.0f);
            //Gl.glBegin(Gl.GL_LINES);
            //Gl.glVertex3f(-dimension, 0.0f, 0.0f);
            //Gl.glVertex3f(dimension, 0.0f, 0.0f);
            //Gl.glEnd();

            Gl.glBegin(Gl.GL_TRIANGLES);

            // Front
            Gl.glColor3f(0.0f, 1.0f , 1.0f);
            Gl.glVertex3f(0.0f, 1.0f * dimension, 0.0f * dimension);
            Gl.glColor3f(0.0f, 0.0f , 1.0f );
            Gl.glVertex3f(-1.0f * dimension, -1.0f * dimension, 1.0f * dimension);
            Gl.glColor3f(0.0f, 0.0f, 0.0f);
            Gl.glVertex3f(1.0f * dimension, -1.0f * dimension, 1.0f * dimension);

            // Right Side Facing Front
            Gl.glColor3f(0.0f, 1.0f , 1.0f);
            Gl.glVertex3f(0.0f, 1.0f * dimension, 0.0f * dimension);
            Gl.glColor3f(0.0f, 0.0f, 1.0f );
            Gl.glVertex3f(1.0f * dimension, -1.0f * dimension, 1.0f * dimension);
            Gl.glColor3f(0.0f, 0.0f, 0.0f);
            Gl.glVertex3f(0.0f, -1.0f * dimension, -1.0f * dimension);

            // Left Side Facing Front
            Gl.glColor3f(0.0f, 1.0f, 1.0f );
            Gl.glVertex3f(0.0f, 1.0f * dimension, 0.0f);
            Gl.glColor3f(0.0f, 0.0f, 1.0f);
            Gl.glVertex3f(0.0f, -1.0f * dimension, -1.0f * dimension);
            Gl.glColor3f(0.0f, 0.0f, 0.0f);
            Gl.glVertex3f(-1.0f * dimension, -1.0f * dimension, 1.0f * dimension);

            // Bottom
            Gl.glColor3f(0.0f, 0.0f, 0.0f);
            Gl.glVertex3f(-1.0f * dimension, -1.0f * dimension, 1.0f * dimension);
            Gl.glColor3f(0.1f, 0.1f , 0.1f);
            Gl.glVertex3f(1.0f * dimension, -1.0f * dimension, 1.0f * dimension);
            Gl.glColor3f(0.2f, 0.2f, 0.2f);
            Gl.glVertex3f(0.0f, -1.0f * dimension, -1.0f * dimension);

            Gl.glEnd();
            // From NeHe lesson 18.

            //Glu.GLUquadric quadric = Glu.gluNewQuadric();
            //try
            //{
            //    Glu.gluQuadricDrawStyle(quadric, drawStyle);
            //    Glu.gluQuadricNormals(quadric, Glu.GLU_SMOOTH);
            //    Glu.gluSphere(quadric, radius, 40, 40);
            //}
            //finally
            //{
            //    Glu.gluDeleteQuadric(quadric);
            //}

            Gl.glPopMatrix();
        }

        #region IDrawable Members


        public System.Drawing.Rectangle DrawSizeRectangle()
        {
            Rectangle rect = new Rectangle((int)eulerRigidBody.ScreenCoordinate.X,
                (int)eulerRigidBody.ScreenCoordinate.Y, dimension, dimension);
            return rect;
        }

        #endregion
    }
}
